// pages/tuan/moreComment/moreComment.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        content: '',
        content1: '',
        commentId: undefined,
        focus: false,
        showModal: false,
        voteId: undefined,
        appraiseList1: [
            {
              username: "太白子",
              img: "/images/banggou.jpg",
              is_merchant: 0,
              isOpen: false,
              change: false,
              praise: 0,
              appraise_content: "上期的大米可以再团一次吗🎺上期的大米可以再团一次吗🎺上期的大米可以再团一次吗🎺上期的大米可以再团一次吗",
              reply_list: [
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25"},
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
              ],
              time: "今天 15:07",
            },
            {
              username: "太白子",
              img: "/images/banggou.jpg",
              is_merchant: 1,
              isOpen: false,
              change: false,
              praise: 0,
              appraise_content: "我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬",
              reply_list: [
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" }
              ],
              time: "今天 15:07",
            },
            {
                username: "太白子",
                img: "/images/banggou.jpg",
                is_merchant: 1,
                isOpen: false,
                change: false,
                praise: 0,
                appraise_content: "我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬",
                reply_list: [
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" }
                ],
                time: "今天 15:07",
              },
              {
                username: "太白子",
                img: "/images/banggou.jpg",
                is_merchant: 1,
                isOpen: false,
                change: false,
                praise: 0,
                appraise_content: "我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬",
                reply_list: [
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" }
                ],
                time: "今天 15:07",
              },
              {
                username: "太白子",
                img: "/images/banggou.jpg",
                is_merchant: 1,
                isOpen: false,
                change: false,
                praise: 0,
                appraise_content: "我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬",
                reply_list: [
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
                  { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" }
                ],
                time: "今天 15:07",
              }
            
          ],
          

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      
      this.setData({
        voteId: options.voteId
      })
      this.initData(this.data.voteId);

    },

    initData: function(voteId){
      app.sendGetRequest('/community/helphelp/voteComments?voteId='+voteId, (res) => {
        
        this.setData({
          appraiseList: res.data
        })
      }, (err) => {
        console.log(err.errMsg)
      })

    },


    //失去焦点时获取里面评论内容
  bindTextAreaBlur: function (e) {
    this.setData({
      content: e.detail.value,
    })
  },
  //点击按钮时得到里面的值
  fabiao: function (e) {
    if(this.data.content == '') {
      wx.showToast({
        title: '内容不能为空',
        icon: "none",
        duration: 1500,
      })
    }else {
      this.setData({
        focus: 'false',
        concent1: this.data.content,
      })
      console.log(this.data.content)
    }
  },
  /**
   * 点击回复显示隐藏评价框
   */
  chengeStatusTop: function() {
    let status = this.data.status;
    this.setData({
      status: !status
    })
  },
  // 点赞功能逻辑
  praiseThis: function (e) {
    var index = e.currentTarget.dataset.curindex;
    if (this.data.repotList[index]) {
      var change = this.data.repotList[index].change;
      if (change !== undefined) {
        var num = this.data.repotList[index].like;
        if (change) {
          this.data.repotList[index].like = (num - 1);
          this.data.repotList[index].change = false;
        } else {
          this.data.repotList[index].like = (num + 1);
          this.data.repotList[index].change = true;
        }
        this.setData({
          repotList: this.data.repotList
        })
      }
    }
  },
  // 点击展开
  chooseUnfold: function(e) {
    var key = e.currentTarget.dataset.key;
    var val = e.currentTarget.dataset.value;
    key = key + '.isOpen';
    this.setData({
      [key]: !val
    })
  },
  // 点赞功能逻辑s
  praiseThiss: function (e) {
    

    var index = e.currentTarget.dataset.curindex;
    if (this.data.appraiseList[index]) {
      var change = this.data.appraiseList[index].change;
      if (change !== undefined) {
        var num = this.data.appraiseList[index].like ;
        if (change) {
          this.data.appraiseList[index].like  = (num - 1);
          this.data.appraiseList[index].change = false;

          app.sendPostRequest('/community/helphelp/deleteVoteComment1Like', this.data.appraiseList[index],(res) => {
            if(res.code == 200) {
              wx.showToast({
                  title: '取消成功！',  
                  icon: 'success',  
                  duration: 1500
              })
            } else {
              wx.showToast({
                  title: '取消失败！',  
                  icon: 'error',  
                  duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        } else {
          this.data.appraiseList[index].like  = (num + 1);
          this.data.appraiseList[index].change = true;
          // let data = {
          //   commentId: this.data.appraiseList[index].comment1Id,
          // }
          app.sendPostRequest('/community/helphelp/voteComment1Like', this.data.appraiseList[index],(res) => {
            if(res.code == 200) {
              wx.showToast({
                  title: '点赞成功！',  
                  icon: 'success',  
                  duration: 1500
              })
            } else {
              wx.showToast({
                  title: '点赞失败！',  
                  icon: 'error',  
                  duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        
        }
        
        this.setData({
          appraiseList: this.data.appraiseList
        })
      }
    }







  },
  // 点赞内层逻辑
  praiseThisss: function(e) {
    var index = e.currentTarget.dataset.curindex;
    var indexs = e.currentTarget.dataset.curindexs;
    console.log(indexs)
    if (this.data.appraiseList[index].replyList[indexs]) {
      
      var change = this.data.appraiseList[index].replyList[indexs].changes;
      console.log(change)
      if (change !== undefined) {
        var num = this.data.appraiseList[index].replyList[indexs].like;
        if (change) {
          this.data.appraiseList[index].replyList[indexs].like = (num - 1);
          this.data.appraiseList[index].replyList[indexs].changes = false;
          app.sendPostRequest('/community/helphelp/deleteVoteComment2Like', this.data.appraiseList[index].replyList[indexs],(res) => {
            if(res.code == 200) {
              wx.showToast({
                  title: '取消成功！',  
                  icon: 'success',  
                  duration: 1500
              })
            } else {
              wx.showToast({
                  title: '取消失败！',  
                  icon: 'error',  
                  duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        } else {
          this.data.appraiseList[index].replyList[indexs].like = (num + 1);
          this.data.appraiseList[index].replyList[indexs].changes = true;
          app.sendPostRequest('/community/helphelp/voteComment2Like', this.data.appraiseList[index].replyList[indexs],(res) => {
            if(res.code == 200) {
              wx.showToast({
                  title: '点赞成功！',  
                  icon: 'success',  
                  duration: 1500
              })
            } else {
              wx.showToast({
                  title: '点赞失败！',  
                  icon: 'error',  
                  duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        }
        this.setData({
          appraiseList: this.data.appraiseList
        })
      }
    }
    //test/test.js

   
  },
  

    //评论
    bindReply: function(e) {
      this.setData({
        focus: true
      });
    },
    contentInput:function(e) { //当输入框的值发生改变时，获取
      this.setData({
        content: e.detail.value
      });
    },

    inputChange: function(e){
      this.setData({
        content1:e.detail.value
      });
    },
    comment: function(){
      console.log(this.data.content)
      let data = {
        content: this.data.content,
        voteId: this.data.voteId
      }
      app.sendPostRequest('/community/helphelp/voteComment1', data,(res) => {
        if(res.code == 200) {
          // that.setData({
          //   [s]: false
          // })
          wx.showToast({
              title: '评论成功！',  
              icon: 'success',  
              duration: 1500
          })
          this.setData({
            content: ' '
          });
          this.initData(this.data.voteId);
        } else {
          wx.showToast({
              title: '评论失败！',  
              icon: 'error',  
              duration: 1500
          })
        }

      })},
      /**
     * 弹窗
     */
    showDialogBtn: function(e) {
      var index = e.currentTarget.dataset.index;
      console.log(index);
      if (this.data.appraiseList[index]) {
        this.setData({
          showModal: true,
          commentId: this.data.appraiseList[index].commentId
        })

      }
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {
    },
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
      this.setData({
        showModal: false
      });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function () {
      this.hideModal();
    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function () {
      console.log(this.data.content)
      let data = {
        content: this.data.content1,
        comment1Id: this.data.commentId
      }
      app.sendPostRequest('/community/helphelp/voteComment2', data,(res) => {
        if(res.code == 200) {
          // that.setData({
          //   [s]: false
          // })
          wx.showToast({
              title: '回复成功！',  
              icon: 'success',  
              duration: 1500
          })
          this.setData({
            content1: ' '
          });
          this.initData(this.data.voteId);
        } else {
          wx.showToast({
              title: '评论失败！',  
              icon: 'error',  
              duration: 1500
          })
        }

      })
      this.hideModal();
    }


  })